package com.gaga.common.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Base64;

public class AesAiobsUtils {
    //真实
    public static final String APP_KEY = "bb97a02e-4908-4a09-a3fd-17ddd68f5bfd";
    public static final String APP_SECRET = "a1b210be-953c-4b15-8e20-563496e9d38d";
    //弹幕助手
//    public static final String APP_KEY = "416f62b5-4c87-4518-ab04-94f36c6723ce";
//    public static final String APP_SECRET = "eae44fbe-33db-4297-b746-34b2adb57177";

    public static String verifyUrl = "https://api.aiobs.cn/aiobs/api/activeCode/verify";

    /**
     * AES加密（自动生成IV，无需外部输入）
     *
     * @param plainText 待加密的明文
     * @return Base64编码的密文（含IV）
     */
    public static String encrypt(String plainText) throws Exception {
        // 生成AES密钥
        byte[] keyBytes = getKey();
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");

        // 初始化AES实例
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        // 获取IV并拼接在密文前
        byte[] iv = cipher.getIV();
        byte[] encryptedData = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));

        // 合并IV和加密数据
        byte[] combined = new byte[iv.length + encryptedData.length];
        System.arraycopy(iv, 0, combined, 0, iv.length);
        System.arraycopy(encryptedData, 0, combined, iv.length, encryptedData.length);

        return Base64.getEncoder().encodeToString(combined);
    }

    /**
     * AES解密（自动提取IV，无需外部输入）
     *
     * @param cipherText Base64编码的密文（含IV）
     * @return 解密后的明文
     */
    public static String decrypt(String cipherText) throws Exception {
        // 解析Base64并分离IV和加密数据
        byte[] combined = Base64.getDecoder().decode(cipherText);
        byte[] iv = new byte[16];
        System.arraycopy(combined, 0, iv, 0, iv.length);
        byte[] encryptedData = new byte[combined.length - iv.length];
        System.arraycopy(combined, iv.length, encryptedData, 0, encryptedData.length);

        // 生成AES密钥
        byte[] keyBytes = getKey();
        SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES");

        // 初始化AES实例
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));

        // 解密数据
        byte[] decryptedData = cipher.doFinal(encryptedData);
        return new String(decryptedData, StandardCharsets.UTF_8);
    }

    /**
     * 将任意字符串转换为合法AES密钥（SHA256哈希）
     */
    private static byte[] getKey() throws Exception {
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        return sha256.digest(APP_SECRET.getBytes(StandardCharsets.UTF_8));
    }


    public static void main(String[] args) throws Exception {
        // 弹幕助手
//        String activeCode = "e740dd00-a9b8-45d4-aaa3-c0bc65feb07b";
        //真实
        String activeCode = "2ec76b06-4796-4af2-b286-efce4760437c";

        String encrypted = encrypt(activeCode);
        System.out.println("加密结果: " + encrypted);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AppKey", APP_KEY);
        jsonObject.put("ActiveCode", encrypted);

        String post = HttpUtil.post("https://api.aiobs.cn/aiobs/api/activeCode/verify", JSON.toJSONString(jsonObject), 30000);
        System.out.println(post);
    }


}
